package com.cmccgz.udacs_landlord_webapp.main.host;


import android.content.Context;

import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.bean.DeviceInfoBean;
import com.cmccgz.udacs_landlord_webapp.bean.HostMenuBean;
import com.cmccgz.udacs_landlord_webapp.bean.PushRequestBean;
import com.cmccgz.udacs_landlord_webapp.bean.ResultBean;
import com.cmccgz.udacs_landlord_webapp.bean.WebBean;
import com.cmccgz.udacs_landlord_webapp.data.DevicesDataSource;
import com.cmccgz.udacs_landlord_webapp.data.DevicesRepository;
import com.cmccgz.udacs_landlord_webapp.data.PushRequestListDataSource;
import com.cmccgz.udacs_landlord_webapp.data.PushRequestRepository;
import com.cmccgz.udacs_landlord_webapp.main.accreditmanager.AccreditMngActivity;
import com.cmccgz.udacs_landlord_webapp.main.housemanager.manager.HouseManagerActivity;
import com.cmccgz.udacs_landlord_webapp.main.unlock.UnlockRecordActivity;
import com.cmccgz.udacs_landlord_webapp.main.warning.WarningActivity;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class HostPresenter implements HostContract.Presenter {
    private final DevicesRepository devicesRepository;
    private final PushRequestRepository pushRequestRepository;
    private HostContract.View view;

    public HostPresenter(HostContract.View view) {
        this.view = view;
        view.setPresenter(this);
        devicesRepository = new DevicesRepository();
        pushRequestRepository = new PushRequestRepository();
    }

    @Override
    public void initData(Context mContext) {
        final List<HostMenuBean> menuDatas = new ArrayList<>();
        final List<DeviceInfoBean> deviceList = new ArrayList<>();
        final List<WebBean> bannerDatas=new ArrayList<>();

        //menuDatas.add(new HostMenuBean(CardAccreditActivity.class, "app授权", R.drawable.ic_card));
        menuDatas.add(new HostMenuBean(AccreditMngActivity.class, "授权管理", R.drawable.ic_card));
        menuDatas.add(new HostMenuBean(UnlockRecordActivity.class, "进出记录", R.drawable.ic_passout));
        menuDatas.add(new HostMenuBean(WarningActivity.class/*null*/, "设备报障", R.drawable.ic_warning));
//        menuDatas.add(new HostMenuBean(/*PeopleManagerActivity.class*/null, "租客管理", R.drawable.ic_man));
        menuDatas.add(new HostMenuBean(HouseManagerActivity.class, "房屋管理", R.drawable.ic_house));
//        menuDatas.add(new HostMenuBean(null, "社区管理", R.drawable.ic_shequ));
        menuDatas.add(new HostMenuBean(HostMenuBean.EmptyMenu.class, "", 0));
        menuDatas.add(new HostMenuBean(HostMenuBean.EmptyMenu.class, "", 0));
        //第一次拿菜单数据
        view.getdata(menuDatas,deviceList,bannerDatas);
        //开始获取设备数据
        devicesRepository.getDevices(new DevicesDataSource.OnGetDevicesLisenter() {
            @Override
            public void getDevicesCache(List<DeviceInfoBean> datas) {
                addAllDevices(datas);
                view.getdata(menuDatas,deviceList,bannerDatas);

            }

            @Override
            public void getDevicesSuccess(List<DeviceInfoBean> datas) {
                addAllDevices(datas);
                view.getdata(menuDatas,deviceList,bannerDatas);
            }


            public void addAllDevices(List<DeviceInfoBean> datas) {
                deviceList.clear();
                if (datas!=null) {
                    deviceList.addAll(datas);
                }
            }

            @Override
            public void getDevicesError(String msg) {

            }
        });
        //开始获取广告数据
        bannerDatas.add(new WebBean("http://www.gd.10086.cn/upfile/uploader/advert/2017-03/20170331143054052-222.jpg",
                "广告","http://gd.10086.cn/pmarketing/ydcy/index.shtml?WT.inner_id=GZYHLDSX032100I64051"));
        bannerDatas.add(new WebBean("http://www.gd.10086.cn/upfile/uploader/advert/2017-08/20170818150023304-306.jpg",
                "广告","http://gd.10086.cn/pmarketing/preferential/mrk/mrkinfo/tdk.jsps?marketsid=8873&WT.inner_id=GZYHLYZK081800I64031"));
        view.getdata(menuDatas,deviceList,bannerDatas);
    }

    @Override
    public void getDevices() {
//        ApiService.getDevices(mobile).subscribeOn(Schedulers.newThread())
//                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<ResultBean<List<Device>>>() {
//            @Override
//            public void onCompleted() {
//
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                view.showErrorMsg("获取门禁设备失败");
//            }
//
//            @Override
//            public void onNext(ResultBean<List<Device>> listResultBean) {
//                if ("0".equals(listResultBean.rsmsg.rscode) && listResultBean.resp != null) {
//                    List<Device> deviceList = listResultBean.resp;
//                    view.getDevicesSuccess(deviceList);
//                } else {
//                    view.showErrorMsg(listResultBean.rsmsg.rsdesc);
//                }
//            }
//        });

        devicesRepository.getDevices(new DevicesDataSource.OnGetDevicesLisenter() {
            @Override
            public void getDevicesCache(List<DeviceInfoBean> datas) {
                view.getDevicesCache(datas);
            }

            @Override
            public void getDevicesSuccess(List<DeviceInfoBean> datas) {
                view.getDevicesSuccess(datas);
            }

            @Override
            public void getDevicesError(String msg) {

            }
        });
    }

    @Override
    public void openDoor(String mobile, String deviceId) {
        ApiService.openDoor(mobile, deviceId).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<ResultBean>() {
            @Override
            public void onCompleted() {
                view.dismissLoadingDialog();
            }

            @Override
            public void onError(Throwable e) {
                view.dismissLoadingDialog();
                view.showErrorMsg("远程开门失败");
            }

            @Override
            public void onNext(ResultBean resultBean) {
                if ("0".equals(resultBean.rsmsg.rscode)) {
                    view.openDoorSuccess();
                } else {
                    view.showErrorMsg(resultBean.rsmsg.rsdesc);
                }
            }
        });
    }

    @Override
    public void getReqList(String q_word, String o_cond, String page) {
        pushRequestRepository.getRequestList(q_word, o_cond, page, new PushRequestListDataSource.PushRequestListLisenter() {
            @Override
            public void getReqListSuccess(List<PushRequestBean> datas) {
                boolean isNoAccredited=false;
                for (int i = 0; datas!=null&&i < datas.size(); i++) {
                    if (isNoAccredited=!datas.get(i).has_use)
                        break;
                }
                view.getReqListSuccess(datas,isNoAccredited);
            }

            @Override
            public void getReqListError(String msg) {
                view.getReqListError(msg);
            }
        });
    }

    @Override
    public void start() {

    }
}
